package org.hzz.locks;

import lombok.extern.slf4j.Slf4j;

import java.util.Scanner;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.locks.LockSupport;

/**
 * @author <a href="mailto:1193094618@qq.com">pkmer</a>
 * <br>
 * <a href = "https://gitee.com/developeros/videos-online">Code Repository</a>
 * At 2024/9/7
 */
@Slf4j
public class TestMain {

    public static Thread mainThread;

    public static void main(String[] args) throws InterruptedException {

        Runnable runnable = () -> {
            Thread thread = null;
            try{
                thread = Thread.currentThread();
                log.info("start thread id: {}",thread.getId());
                Scanner scanner = new Scanner(System.in);
                scanner.nextLine();
//                thread.stop();
                System.exit(0);
                System.out.println("end");
            }catch (Exception | Error e){
                e.printStackTrace();
            }
            finally {
                log.info("finally thread id: {}",thread.getId());
                System.out.println("Run????");
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
        log.info(" id: {}",thread.getId());

    }

    public void t1() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(1);
        CompletableFuture.runAsync(() -> {
            try{
                System.out.println("执行业务方法");
            }finally {

//                unlock();
//                latch.countDown();

                try{
                    unlock();
                }finally {
                    latch.countDown();
                }
            }
        });

        latch.await();
        System.out.println("over");
    }

    public static void unlock(){
        CompletableFuture<Void> future = CompletableFuture.runAsync(()->{
            throw new RuntimeException("xxx");
        }).whenComplete((res,e) -> {
            throw new RuntimeException("111");
        });
        try {
            future.get();
        }catch(Exception e){
            throw new RuntimeException(e);
        }
    }

    public void testIntegerNull(){
        Integer i = null;

        if(i == 1){
            System.out.println(i);
        }else{
            System.out.println("我执行了");
        }
    }

    public void run(){
        log.info("hello world");
        mainThread = Thread.currentThread();

        anotherThread();

        //LockSupport.park(); // 此时线程被阻塞
        LockSupport.parkNanos(TestMain.class,1000); // 此时线程被阻塞
        log.info("mainThread is interrupted: {}",mainThread.isInterrupted());
        log.info("{} wake up",mainThread.getName());
    }

    public static void anotherThread(){
        CompletableFuture.runAsync(() -> {
            log.info("Enter to wake up main");
            Scanner scanner = new Scanner(System.in);
            scanner.nextLine();
//            LockSupport.unpark(mainThread);
            mainThread.interrupt();
        });
    }
}
